package com.yxjx.duoxue.comment;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.datamodel.DMComment;
import com.yxjx.duoxue.utils.Utils;
import com.yxjx.duoxue.utils.UIHelper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

public class CommentItemView extends LinearLayout {
	
	public static final int MODE_COMMENT_LIST = 0; // 点评详情中的控件
	public static final int MODE_COMMENT_FUNCTION = 1; // 课程／学校介绍中的控件

	private View mRoot;
	private TextView mViewReplies;
	private CommentRespondsListView mAllReplies;
	private ExpandableTextView mCommentText;
	private AllThumbnailsView mAllThumbnailsView;
	private View mBottomLine;

	public CommentItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		mRoot = LayoutInflater.from(context).inflate(R.layout.item_comment_record, this, true);
		mViewReplies = (TextView) mRoot.findViewById(R.id.view_responds);
		mAllReplies = (CommentRespondsListView) mRoot.findViewById(R.id.comment_respond_list);
		mCommentText = (ExpandableTextView) mRoot.findViewById(R.id.comment_text);
		mAllThumbnailsView = (AllThumbnailsView) mRoot.findViewById(R.id.all_images);
		mUserName = (TextView) mRoot.findViewById(R.id.user_name);
		mCommentDate = (TextView) mRoot.findViewById(R.id.comment_date);
		mRating = (RatingBar) mRoot.findViewById(R.id.rating_bar);
		mBottomLine = mRoot.findViewById(R.id.bottom_divider);
		// click event handling.
		mViewReplies.setOnClickListener(mClickListener);
	}
	
	public CommentItemView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	private View.OnClickListener mClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.view_responds:
				if (mAllReplies.getVisibility() != View.VISIBLE) {
					mAllReplies.setVisibility(View.VISIBLE);
				} else {
					mAllReplies.setVisibility(View.GONE);
				}
				break;
			default:
				break;
			}
		}
	};
	private TextView mUserName;
	private TextView mCommentDate;
	private RatingBar mRating;
	
	// for listview re-use.
	public void reset() {
		
	}
	
	// for listview initialization
	public void update() {
		
	}
	
	// init 
	public void setComment(DMComment comment) {
		mUserName.setText(Utils.maskCell(comment.getContactPhone()));
		mCommentDate.setText(Utils.getTimeStamp(comment.getCreateTime(), "yyyy-MM-dd"));
		mRating.setRating(comment.getLevel().getTotalLevel());
		mCommentText.setText(comment.getCommentText());
		mCommentText.requestLayout();
		mAllReplies.setData(comment.getReplys());
		mAllReplies.setVisibility(View.GONE);
		mAllThumbnailsView.setData(comment.getImages());
		boolean shouldDismissFooter = (comment.getReplys() == null || comment.getReplys().size() == 0);
		mViewReplies.setVisibility(shouldDismissFooter ? View.GONE : View.VISIBLE);
	}
	
	/**
	 * 将课程，学校介绍页面的该控件设置为不可展开，不显示回复信息。
	 */
	public void setAsCommentFunctionMode() {
		mCommentText.setExpandable(false);
		mCommentText.setClickable(false);
		mViewReplies.setVisibility(View.GONE);
		mAllReplies.setVisibility(View.GONE);
		mBottomLine.setVisibility(View.GONE);
	}
}
